﻿namespace Sidvall.WorkItemManager.UI.Entities
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
    public sealed partial class WorkItemLog : global::Sidvall.WorkItemManager.UI.Entities.Core.BusinessBase, global::Sidvall.WorkItemManager.Data.IWorkItemLog
    {
        #region Business Members

        #region ExecutionEndTime

        private System.DateTime _ExecutionEndTime = System.DateTime.UtcNow;
        partial void OnBeforeExecutionEndTimePropertyGet(Sidvall.Business.PropertyEventData<System.DateTime> eventData);
        partial void OnBeforeExecutionEndTimePropertyChanged(Sidvall.Business.PropertyChangeEventData<System.DateTime> eventData);
        partial void OnAfterExecutionEndTimePropertyChanged();
        public System.DateTime ExecutionEndTime
        {
            get
            {
                Sidvall.Business.PropertyEventData<System.DateTime> eventData = new Sidvall.Business.PropertyEventData<System.DateTime>(Sidvall.Business.ValidationManager.GetValidDateTime(_ExecutionEndTime));
                OnBeforeExecutionEndTimePropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<System.DateTime> eventData = new Sidvall.Business.PropertyChangeEventData<System.DateTime>(value, _ExecutionEndTime);
                OnBeforeExecutionEndTimePropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("ExecutionEndTime", eventData.Value, true, true, true);
                    OnAfterExecutionEndTimePropertyChanged();
                }
            }
        }

        #endregion
        #region ExecutionStartTime

        private System.DateTime _ExecutionStartTime = System.DateTime.UtcNow;
        partial void OnBeforeExecutionStartTimePropertyGet(Sidvall.Business.PropertyEventData<System.DateTime> eventData);
        partial void OnBeforeExecutionStartTimePropertyChanged(Sidvall.Business.PropertyChangeEventData<System.DateTime> eventData);
        partial void OnAfterExecutionStartTimePropertyChanged();
        public System.DateTime ExecutionStartTime
        {
            get
            {
                Sidvall.Business.PropertyEventData<System.DateTime> eventData = new Sidvall.Business.PropertyEventData<System.DateTime>(Sidvall.Business.ValidationManager.GetValidDateTime(_ExecutionStartTime));
                OnBeforeExecutionStartTimePropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<System.DateTime> eventData = new Sidvall.Business.PropertyChangeEventData<System.DateTime>(value, _ExecutionStartTime);
                OnBeforeExecutionStartTimePropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("ExecutionStartTime", eventData.Value, true, true, true);
                    OnAfterExecutionStartTimePropertyChanged();
                }
            }
        }

        #endregion
        #region ResultMessage

        private string _ResultMessage = string.Empty;
        partial void OnBeforeResultMessagePropertyGet(Sidvall.Business.PropertyEventData<string> eventData);
        partial void OnBeforeResultMessagePropertyChanged(Sidvall.Business.PropertyChangeEventData<string> eventData);
        partial void OnAfterResultMessagePropertyChanged();
        public string ResultMessage
        {
            get
            {
                Sidvall.Business.PropertyEventData<string> eventData = new Sidvall.Business.PropertyEventData<string>(_ResultMessage);
                OnBeforeResultMessagePropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<string> eventData = new Sidvall.Business.PropertyChangeEventData<string>(value, _ResultMessage);
                OnBeforeResultMessagePropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("ResultMessage", eventData.Value, true, true, true);
                    OnAfterResultMessagePropertyChanged();
                }
            }
        }

        #endregion
        #region WorkItemId

        private long _WorkItemId = 0L;
        partial void OnBeforeWorkItemIdPropertyGet(Sidvall.Business.PropertyEventData<long> eventData);
        partial void OnBeforeWorkItemIdPropertyChanged(Sidvall.Business.PropertyChangeEventData<long> eventData);
        partial void OnAfterWorkItemIdPropertyChanged();
        public long WorkItemId
        {
            get
            {
                Sidvall.Business.PropertyEventData<long> eventData = new Sidvall.Business.PropertyEventData<long>(_WorkItemId);
                OnBeforeWorkItemIdPropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<long> eventData = new Sidvall.Business.PropertyChangeEventData<long>(value, _WorkItemId);
                OnBeforeWorkItemIdPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("WorkItemId", eventData.Value, true, true, true);
                    OnAfterWorkItemIdPropertyChanged();
                }
            }
        }

        #endregion
        #region WorkItemLogId

        private System.Nullable<long> _WorkItemLogId = null;
        partial void OnBeforeWorkItemLogIdPropertyGet(Sidvall.Business.PropertyEventData<System.Nullable<long>> eventData);
        partial void OnBeforeWorkItemLogIdPropertyChanged(Sidvall.Business.PropertyChangeEventData<System.Nullable<long>> eventData);
        partial void OnAfterWorkItemLogIdPropertyChanged();
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public System.Nullable<long> WorkItemLogId
        {
            get
            {
                Sidvall.Business.PropertyEventData<System.Nullable<long>> eventData = new Sidvall.Business.PropertyEventData<System.Nullable<long>>(_WorkItemLogId);
                OnBeforeWorkItemLogIdPropertyGet(eventData);
                return eventData.Value;
            }
            internal set
            {
                Sidvall.Business.PropertyChangeEventData<System.Nullable<long>> eventData = new Sidvall.Business.PropertyChangeEventData<System.Nullable<long>>(value, _WorkItemLogId);
                OnBeforeWorkItemLogIdPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("WorkItemLogId", eventData.Value, true, true, true);
                    OnAfterWorkItemLogIdPropertyChanged();
                }
            }
        }

        #endregion
        #region WorkItemResultId

        private global::Sidvall.WorkItemManager.Workflow.WorkItemResultType _WorkItemResultId = global::Sidvall.WorkItemManager.Workflow.WorkItemResultType.None;
        partial void OnBeforeWorkItemResultIdPropertyGet(Sidvall.Business.PropertyEventData<global::Sidvall.WorkItemManager.Workflow.WorkItemResultType> eventData);
        partial void OnBeforeWorkItemResultIdPropertyChanged(Sidvall.Business.PropertyChangeEventData<global::Sidvall.WorkItemManager.Workflow.WorkItemResultType> eventData);
        partial void OnAfterWorkItemResultIdPropertyChanged();
        public global::Sidvall.WorkItemManager.Workflow.WorkItemResultType WorkItemResultId
        {
            get
            {
                Sidvall.Business.PropertyEventData<global::Sidvall.WorkItemManager.Workflow.WorkItemResultType> eventData = new Sidvall.Business.PropertyEventData<global::Sidvall.WorkItemManager.Workflow.WorkItemResultType>(_WorkItemResultId);
                OnBeforeWorkItemResultIdPropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<global::Sidvall.WorkItemManager.Workflow.WorkItemResultType> eventData = new Sidvall.Business.PropertyChangeEventData<global::Sidvall.WorkItemManager.Workflow.WorkItemResultType>(value, _WorkItemResultId);
                OnBeforeWorkItemResultIdPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("WorkItemResultId", eventData.Value, true, true, true);
                    OnAfterWorkItemResultIdPropertyChanged();
                }
            }
        }

        #endregion
        #region WorkItem

        private WorkItem _WorkItem;
        partial void OnBeforeWorkItemPropertyChanged(Sidvall.Business.PropertyChangeEventData<WorkItem> eventData);
        partial void OnAfterWorkItemPropertyChanged();
        public WorkItem WorkItem
        {
            get
            {
                return _WorkItem;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<WorkItem> eventData = new Sidvall.Business.PropertyChangeEventData<WorkItem>(value, _WorkItem);
                OnBeforeWorkItemPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("WorkItem", eventData.Value, true, true, false);
                    OnAfterWorkItemPropertyChanged();
                }
            }
        }

        #endregion
        #region DataRowContextStorageKey

        protected override string DataRowContextStorageKey
        {
            get
            {
                return global::Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkItemLogManager.GetKey(this);
            }
        }

        #endregion
        #region DataRowIsValid

        public override bool DataRowIsValid(Sidvall.Data.BusinessRuleFilter filter)
        {
            return global::Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkItemLogManager.DataRowIsValid(this, filter);
        }

        #endregion
        #region DataRowStateIsDirty

        public override bool DataRowStateIsDirty
        {
            get
            {
                return global::Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkItemLogManager.DataRowStateIsDirty(this);
            }
        }

        #endregion

        #region LoadWorkItemAsync

        public async System.Threading.Tasks.Task<WorkItem> LoadWorkItemAsync()
        {
            return await LoadWorkItemAsync(false, true, null, null, null);
        }
        public async System.Threading.Tasks.Task<WorkItem> LoadWorkItemAsync(bool refresh, bool raisePropertyChanged, global::Sidvall.Data.FetchCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            await global::Sidvall.WorkItemManager.UI.SystemContext.Current.DataPortal.WorkItemLogManager.LoadWorkItemAsync(this, refresh, raisePropertyChanged, criteria, strategyContext, objectParameters);

            return this.WorkItem;
        }
        public WorkItem LoadWorkItem(WorkItem item)
        {
            LoadInstanceField("WorkItem", item, true, true, false);
            return item;
        }

        #endregion
        #region LoadInstanceField

        public void LoadInstanceField(string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState)
        {
            global::Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkItemLogManager.LoadInstanceField(this, fieldName, value, cascade, raisePropertyChanged, updateState);
        }

        #endregion
        #region AcceptChanges

        public void AcceptChanges()
        {
            global::Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkItemLogManager.AcceptChanges(this);
        }

        #endregion
        #region Clone

        public WorkItemLog Clone()
        {
            var copySettings = Sidvall.Data.CopySettings.Clone();
            return Copy(copySettings);
        }

        #endregion
        #region Copy

        public WorkItemLog Copy()
        {
            return Copy(null);
        }
        public WorkItemLog Copy(Sidvall.Data.CopySettings copySettings)
        {
            if (copySettings == null)
                copySettings = new Sidvall.Data.CopySettings();
            if (!Sidvall.Data.DataRowContextManager.IsValid(this, copySettings.RowFilter))
                return null;
            var item = new WorkItemLog();
            Import(item, this, copySettings);
            return item;
        }

        #endregion
        #region Import

        public void Import(global::Sidvall.WorkItemManager.Data.IWorkItemLog item)
        {
            Import(item, null);
        }
        public void Import(global::Sidvall.WorkItemManager.Data.IWorkItemLog item, Sidvall.Data.CopySettings copySettings)
        {
            Import(this, item, copySettings);
        }
        partial void OnAfterImport(WorkItemLog destination, global::Sidvall.WorkItemManager.Data.IWorkItemLog source, Sidvall.Data.CopySettings copySettings);
        private void Import(WorkItemLog destination, global::Sidvall.WorkItemManager.Data.IWorkItemLog source, Sidvall.Data.CopySettings copySettings)
        {
            global::Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkItemLogManager.Import(destination, source, copySettings);
            OnAfterImport(destination, source, copySettings);
        }
        partial void OnAfterImport(WorkItemLog destination, WorkItemLog source, Sidvall.Data.CopySettings copySettings);
        private void Import(WorkItemLog destination, WorkItemLog source, Sidvall.Data.CopySettings copySettings)
        {
            Import(destination, source as global::Sidvall.WorkItemManager.Data.IWorkItemLog, copySettings);
            OnAfterImport(destination, source, copySettings);
        }

        #endregion
        #region SaveItemAsync

        public async System.Threading.Tasks.Task<WorkItemLog> SaveItemAsync()
        {
            return await global::Sidvall.WorkItemManager.UI.SystemContext.Current.DataPortal.WorkItemLogManager.SaveItemAsync(this);
        }
        public async System.Threading.Tasks.Task<WorkItemLog> SaveItemAsync(Sidvall.Data.SaveMode mode)
        {
            return await global::Sidvall.WorkItemManager.UI.SystemContext.Current.DataPortal.WorkItemLogManager.SaveItemAsync(this, mode);
        }
        public async System.Threading.Tasks.Task<WorkItemLog> SaveItemAsync(Sidvall.Data.SaveSettings settings)
        {
            return await global::Sidvall.WorkItemManager.UI.SystemContext.Current.DataPortal.WorkItemLogManager.SaveItemAsync(this, settings);
        }
        public async System.Threading.Tasks.Task<WorkItemLog> SaveItemAsync(Sidvall.Data.SaveSettings settings, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            return await global::Sidvall.WorkItemManager.UI.SystemContext.Current.DataPortal.WorkItemLogManager.SaveItemAsync(this, settings, strategyContext, objectParameters);
        }

        #endregion
        #region ValidateAsync

        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync()
        {
            return await ValidateAsync((Sidvall.Data.ValidationSettings)null);
        }
        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync(Sidvall.Data.ValidationSettings settings)
        {
            var context = new Sidvall.Business.ValidationContext(settings);
            await global::Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkItemLogManager.ValidateAsync(this, context);
            await ValidateAsync(context);
            return context.AllBusinessRules;
        }
        partial void OnAfterValidate(Sidvall.Data.BusinessRuleCollection itemBusinessRules, Sidvall.Business.ValidationContext context);
        public async System.Threading.Tasks.Task ValidateAsync(Sidvall.Business.ValidationContext context)
        {
            var itemBusinessRules = new Sidvall.Data.BusinessRuleCollection();
            OnAfterValidate(itemBusinessRules, context);
            await context.TaskContext.DoTaskAsync();
            context.AllBusinessRules.AddRange(context.Settings.IncludeNotBrokenRules, itemBusinessRules);
            var businessRulesProvider = this as Sidvall.Data.IBusinessRulesProvider;
            if (businessRulesProvider != null)
                businessRulesProvider.BusinessRules.AddRange(context.Settings.IncludeNotBrokenRules, itemBusinessRules);
        }

        #endregion
        #region Initialize

        public void Initialize()
        {
            global::Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkItemLogManager.Initialize(this);
        }
        public void Initialize(Sidvall.Data.InitializeSettings settings)
        {
            global::Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkItemLogManager.Initialize(this, settings);
        }

        #endregion
        
        #region IWorkItemLog Members

        System.DateTime global::Sidvall.WorkItemManager.Data.IWorkItemLog.ExecutionEndTime
        {
            get
            {
                return this._ExecutionEndTime;
            }
            set
            {
                this._ExecutionEndTime = value;
            }
        }
        System.DateTime global::Sidvall.WorkItemManager.Data.IWorkItemLog.ExecutionStartTime
        {
            get
            {
                return this._ExecutionStartTime;
            }
            set
            {
                this._ExecutionStartTime = value;
            }
        }
        string global::Sidvall.WorkItemManager.Data.IWorkItemLog.ResultMessage
        {
            get
            {
                return this._ResultMessage;
            }
            set
            {
                this._ResultMessage = value;
            }
        }
        long global::Sidvall.WorkItemManager.Data.IWorkItemLog.WorkItemId
        {
            get
            {
                return this._WorkItemId;
            }
            set
            {
                this._WorkItemId = value;
            }
        }
        System.Nullable<long> global::Sidvall.WorkItemManager.Data.IWorkItemLog.WorkItemLogId
        {
            get
            {
                return this._WorkItemLogId;
            }
            set
            {
                this._WorkItemLogId = value;
            }
        }
        global::Sidvall.WorkItemManager.Workflow.WorkItemResultType global::Sidvall.WorkItemManager.Data.IWorkItemLog.WorkItemResultId
        {
            get
            {
                return this._WorkItemResultId;
            }
            set
            {
                this._WorkItemResultId = value;
            }
        }
        global::Sidvall.WorkItemManager.Data.IWorkItem global::Sidvall.WorkItemManager.Data.IWorkItemLog.WorkItem
        {
            get
            {
                return this._WorkItem;
            }
            set
            {
                UpdateChildProperty(this._WorkItem, value);
                this._WorkItem = value as WorkItem;
            }
        }

        #endregion

        #endregion
        #region Constructors

        public WorkItemLog()
        {
        }

        public WorkItemLog(Sidvall.Data.DataRowState? dataRowState)
            : base(dataRowState)
        {
        }

        #endregion
    }
}